package com.hmall.cart.listener;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hmall.cart.domain.po.Cart;
import com.hmall.cart.service.ICartService;
import com.hmall.common.constants.MqConstants;
import com.hmall.common.domain.CartClearMessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class OrderCreatedListener {
    @Autowired
    private ICartService cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(MqConstants.ORDER_CART_CLEAR_QUEUE),
            exchange = @Exchange(value = MqConstants.ORDER_EXCHANGE_NAME, type = ExchangeTypes.DIRECT),
            key = MqConstants.ORDER_CART_CLEAR_KEY
    ))
    public void listenOrderCreated(CartClearMessageDTO dto){
        cartService.remove(Wrappers.<Cart>lambdaQuery()
                .eq(Cart::getUserId, dto.getUserId())
                .in(Cart::getItemId, dto.getItemIds()));
    }
}